package obfuscation;

import cipher.MD5Utils;

import java.io.*;
import java.util.ArrayList;

/**
 * 生成混淆词典
 */
public class ObfuscationDicGenerator {
    public static void main(String[] args) throws IOException {
        char start = 'a';
        char end = 'z';

        char upperStart = 'A';
        char upperEnd = 'Z';
        File obfuscationFile = new File("proguardDictionary.txt");
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(obfuscationFile)));
        writeLetter(start, end, writer);
        writeLetter(upperStart, upperEnd, writer);

        //take md5 string into dictionary
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(obfuscationFile)));
        ArrayList<String> lines = new ArrayList<>();
        String lineString;
        while ((lineString = reader.readLine()) != null) lines.add(lineString);

        System.out.println("line size: " + lines.size());

        ArrayList<String> md5ObfuscationStringList = new ArrayList<>(lines);
        for (String line : lines) {
            String md5String = MD5Utils.getMD5String(line);
            String obfuscationStr = md5String.substring(0, 15);
            if (!md5ObfuscationStringList.contains(obfuscationStr)) {
                writer.write(obfuscationStr);
                writer.write("\n");
                writer.flush();
                md5ObfuscationStringList.add(obfuscationStr);
            }
        }
    }

    private static void writeLetter(char upperStart, char upperEnd, BufferedWriter writer) throws IOException {
        for (int i = upperStart; i <= upperEnd; i++) {
            for (int j = 1; j <= 10; j++) {
                for (int k = 0; k < j; k++) {
                    writer.write(new char[]{(char) i});
                    writer.flush();
                }
                writer.write("\n");
            }
        }
    }

}
